package edu.pe.cayetano.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.beanutils.RowSetDynaClass;

import ws.synopsis.frmwrk.common.BResult;
import edu.pe.cayetano.framework.Configuration;
import edu.pe.cayetano.framework.DAOException;
import edu.pe.cayetano.seccd.constantes.ConstantesSECCD;

public class OperacionesComunesDAO extends BaseDAO{

	public OperacionesComunesDAO(Connection cnx) {
		super(cnx);
	}
	private String 					schema			= Configuration.getSchemaDB();
	
	/**
	 * Metodo que realiza la ejecucion de un query sin filtros 
	 * */
	public BResult listado(StringBuffer query) throws Exception {
		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		ResultSet 				rs 			 	= null;
		RowSetDynaClass 		rsdc			= null;
		try{		
			pstmt = getConnection().prepareStatement(query.toString());
			rs = pstmt.executeQuery();
			ret = new BResult();
			rsdc = new RowSetDynaClass(rs, false);
			ret.setObject(rsdc);
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(ConstantesSECCD.ERROR_RET);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
				rs.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	}
	
	public BResult listadoxParam(StringBuffer query,String codigo) throws Exception {
		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		ResultSet 				rs 			 	= null;
		RowSetDynaClass 		rsdc			= null;
		try{		
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,codigo);
			rs = pstmt.executeQuery();
			ret = new BResult();
			rsdc = new RowSetDynaClass(rs, false);
			ret.setObject(rsdc);
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(ConstantesSECCD.ERROR_RET);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
				rs.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	}
	
	public String obtenerCodigoInsercion(String tabla) throws Exception {
		int codigo=0;
		PreparedStatement		pstmt 		 	= null;
		ResultSet 				rs 			 	= null;
		try{
			StringBuffer query = new StringBuffer();
			query.append("select count(*) from "+schema+tabla);
			System.out.println("valida usuario:"+query.toString());
			pstmt = getConnection().prepareStatement(query.toString());
			rs=pstmt.executeQuery();	
			if(rs.next()){
				codigo = rs.getInt(1)+1;
			}
			System.out.println("codigo generado:"+codigo);
		}catch(Exception ex){
			throw new DAOException(ex.getMessage());	
		}
		return  String.valueOf(codigo);
	}
	
	public int obtenerCodigoInsercionAnualizada(String tabla) throws Exception {
		int 					codigo			=0;
		PreparedStatement		pstmt 		 	= null;
		ResultSet 				rs 			 	= null;
		String 					connectionType	= Configuration.getConnectionDB();
		try{
			StringBuffer query = new StringBuffer();
			if(connectionType.equalsIgnoreCase("DB2")){
				query.append("SELECT COUNT(*) FROM "+schema+tabla+" WHERE YEAR(FECHA_CREACION)=YEAR(CURRENT_TIMESTAMP)");
					
			}else{
				query.append("SELECT COUNT(*) FROM "+schema+tabla+" WHERE YEAR(FECHA_CREACION)=YEAR(CURRENT_DATE)");
			}
			System.out.println("Operaciones Comunes: query:"+query.toString());
			pstmt = getConnection().prepareStatement(query.toString());
			rs=pstmt.executeQuery();	
			if(rs.next()){
				codigo = rs.getInt(1)+1;
			}
		}catch(Exception ex){
			throw new DAOException(ex.getMessage());	
		}
		return  codigo;
	}

}
